package com.example.liuweather.android;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.liuweather.android.gson.Weather;
import com.example.liuweather.android.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.ViewHolder>{
    private List<String> hotCityNameList;
    private List<String> hotCityIdList;
    private Context context;
    // 定义静态内部类
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView hotCityName;
        public ViewHolder(View view) {
            super(view);
            hotCityName = (TextView) view.findViewById(R.id.hot_city_text);
        }
    }

    public RecyAdapter(Context context, List<String> hotCityNameList,List<String> hotCityIdList) {
        this.context = context;
        this.hotCityNameList = hotCityNameList;
        this.hotCityIdList = hotCityIdList;
    }
    // 以下三个方法是需要重写的！
    // 加载子项布局，创建ViewHolder实例
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hot_city_item,parent,false);
        final ViewHolder vh = new ViewHolder(view);
        // 给recyclerView子项绑定点击事件
        vh.hotCityName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = vh.getAdapterPosition();
                Intent intent = new Intent(context, WeatherActivity.class);
                intent.putExtra("weather_id",hotCityIdList.get(pos));
                intent.putExtra("county_name",hotCityNameList.get(pos));
                context.startActivity(intent);
            }
        });
        return vh;
    }

    // 通过索引来获取数据对象，并通过ViewHolder将数据赋值给控件
    // 每个子项被滚动到屏幕内的时候执行
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String name = hotCityNameList.get(position);
        holder.hotCityName.setText(name);
    }
    // 返回数据的长度
    @Override
    public int getItemCount() {
        return hotCityNameList.size();
    }
}
